<template>
	<view class="box" v-if="isShow">
		<image src="https://www.yongshantuan.cn/images/yongshan/jiantou.png" alt=""></image>
		<view>请点击此处</view>
		<view>使用默认浏览器打开</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				isShow: true,
			}
		},
		onLoad(options) {
			let nowDate = new Date();
			let year = nowDate.getFullYear();
			let month = nowDate.getMonth() + 1;
			let day = nowDate.getDate();
			let hour = nowDate.getHours();
			let minute = nowDate.getMinutes();
			let secound = nowDate.getSeconds();
			let dateStr =
				`${year}${month>9?month:'0'+month}${day>9?day:'0'+day}${hour>9?hour:'0'+hour}${minute>9?minute:'0'+minute}${secound>9?secound:'0'+secound}`

			let {
				ids,
				token,
				storeName
			} = options;

			let isWx = typeof window == 'undefined';
			let _this = this;
			if (isWx) {
				getDownloadUrl();
			} else {
				if (navigator.userAgent.toLowerCase().match(/MicroMessenger/i) == "micromessenger") {
					this.isShow = true
				} else {
					getDownloadUrl();
				}
			}


			function getDownloadUrl() {
				_this.isShow = false
				uni.request({
					url: getApp().globalData.BASE_URL + '/hc/hcOrderMerchant/exportXls',
					method: 'get',
					data: {
						type: 'merchant',
						ids
					},
					header: {
						'X-Access-Token': token,
					},
					success(res) {
						console.log('res', res);
						if (isWx) {
							wxDownload(res.data);
						} else {
							download(res.data);
						}
					},
					fail(error) {
						console.log('error', error)
					}
				})
			}
			// 微信小程序的下载
			function wxDownload(url) {
				wx.downloadFile({
					url: url,
					success(res) {
						// 只要服务器有响应数据，就会把响应内容写入文件并进入 success 回调，业务需要自行判断是否下载到了想要的内容
						console.log('下载成功', res);
						const filePath = res.tempFilePath;
						wx.showModal({
							title: '即将打开文件',
							content: '打开文件后，点击右上角按钮，选择保存到手机',
							showCancel: false,
							success: function(res) {
								if (res) {
									wx.navigateBack({
										success: function() {
											wx.openDocument({
												filePath: filePath,
												showMenu: true,
												success: function(res2) {
													console.log(res2, '打开文档成功');
												}
											});
										}
									})
								}
							}
						})
						
					}
				})
			}

			// 普通下载
			function download(res) {
				let link = document.createElement('a')
				link.style.display = 'none'
				link.href = res.data;
				link.click()
				document.body.removeChild(link); //下载完成移除元素
				return;

				// let url = window.URL.createObjectURL(new Blob([res.data], {
				// 	type: 'application/vnd.ms-excel',
				// }))
				// let link = document.createElement('a')
				// link.style.display = 'none'
				// link.href = url
				// link.setAttribute('download', storeName+'-' + dateStr + '.xls')
				// document.body.appendChild(link)
				// link.click()
				// document.body.removeChild(link); //下载完成移除元素
				// window.URL.revokeObjectURL(url); //释放掉blob对象
			}
		},

		methods: {
			async getDownloadUrl() {

			},
		}
	}
</script>

<style lang="scss" scoped>
	.box {
		background-color: rgba(0, 0, 0, 0.55);
		height: 100vh;
		width: 100%;

		image {
			width: 35%;
			margin-left: 60%;
			margin-top: 20rpx;
		}

		view {
			margin-top: 20rpx;
			color: #fff;
			text-align: center;
			line-height: 1.5;
			font-size: 32rpx;
		}
	}
</style>